﻿
using FairyGUI;

namespace GameMain.Scripts.UI.Framework
{
    public abstract class UIBase
    {
        public GComponent View { get; protected set; }
        
        protected UIPage _page;

        protected bool _open;

        public UIBase(GComponent view) : this(view, UIMgr.GetCurrentPage()) { }
        public UIBase(GComponent view, UIPage page)
        {
            this.View = view;
            view.visible = false;
            _page = page;
        }
        
        public void Open()
        {
            if(_open) return;
            _open = true;
            OnOpen();
            _page.AddChild(this);
            View.visible = true;
        }
        protected virtual void OnOpen(){}

        public void Close()
        {
            if(!_open) return;
            _open = false;
            OnClose();
            _page.RemoveChild(this);
            View.visible = false;
        }
        protected virtual void OnClose(){}

        public void BoolOpen(bool open)
        {
            if (open)
                Open();
            else
                Close();
        }

        public void Dispose()
        {
            OnDispose();
            _page.RemoveChild(this, true);
            View.Dispose();
            View = null;
        }
        protected virtual void OnDispose(){}
    }
}

